package com.douqu.game.main.server.entity;


import com.douqu.game.core.e.E_ServerType;
import com.douqu.game.core.netty.NettyConnection;
import com.douqu.game.main.GameServer;

import java.net.InetSocketAddress;

/**
 * Author : Bean
 * 2017-06-19 17:07
 */
public class ServerInfo {

    protected NettyConnection connection;

    protected String host;

    protected int port;

    protected String httpPort;

    /**
     * 是否已经启动
     */
    protected boolean isRunning;

    protected E_ServerType serverType;



    public ServerInfo(E_ServerType serverType, NettyConnection connection, int port)
    {
        this.serverType = serverType;
        this.connection = connection;
        if(serverType == E_ServerType.BATTLE)
            this.host = GameServer.getInstance().getServerHost();
        else
        {
            InetSocketAddress address = ((InetSocketAddress) connection.getChannel().remoteAddress());
            this.host = address.getHostString();
        }
//        if(ip.indexOf("127.0.0.1") != -1 || ip.indexOf("localhost") != -1)
//        {
//            this.ip = Utils.getInternetIp();
//            if(ip.indexOf("127.0.0.1") != -1 || ip.indexOf("localhost") != -1)
//                this.ip = GameServer.getInstance().getMainServerHost();
//        }
//        if("192.168.46.1".equals(ip)){
//            this.ip = "192.168.2.202";
//        }
        this.port = port;
        this.isRunning = false;
    }

    public void update()
    {

    }




    public void destroy()
    {


    }
    /**
     * 发消息
     * @param code
     * @param data
     */
    public void sendMsg(int code, byte[] data)
    {
        connection.sendMsg(code, data);
//        SendUtils.sendMsg(connection.getChannel(), code, data);
    }



//    public void sendServerStop()
//    {
//        SendUtils.sendMsg(connection.getChannel(), CodeFactory.STOP_SERVER, null);
//    }


//    public int getId()
//    {
//        return port;
//    }



    public boolean isRunning() {
        return isRunning;
    }

    public void setRunning(boolean isRunning) {
        this.isRunning = isRunning;
    }



    public NettyConnection getConnection() {
        return connection;
    }

    public void setConnection(NettyConnection connection) {
        this.connection = connection;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public E_ServerType getServerType() {
        return serverType;
    }

    public void setServerType(E_ServerType serverType) {
        this.serverType = serverType;
    }

    public void setHttpPort(String httpPort) {
        this.httpPort = httpPort;
    }

    public String getHttpPort() {
        return httpPort;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof ServerInfo)) return false;

        ServerInfo that = (ServerInfo) o;

        if (port != that.port) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return port;
    }

    @Override
    public String toString() {
        return "{" +
                "host='" + host + '\'' +
                ", port=" + port +
                ", httpPort=" + httpPort +
                ", serverType=" + serverType +
                '}';
    }
}
